package cn.donave.framework.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public final class ClassHelper {

	private ClassHelper() {
	}

	@SuppressWarnings("unchecked")
	public static final Object getNewObject(String className) {
		if (className == null)
			return null;
		try {
			Class classImpl = Class.forName(className);
			return classImpl.newInstance();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings( { "unchecked" })
	public static final Object getNewConstructorObject(String className,
			Class[] clazzs, Object[] objs) {
		if (className == null)
			return null;
		Class classImpl = null;
		try {
			classImpl = Class.forName(className);
			Constructor Constructor = classImpl.getConstructor(clazzs);
			return Constructor.newInstance(objs);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static String getClassNameByClazz(Class clazz) {
		if (clazz == null)
			return null;
		return clazz.getSimpleName();
	}

}